package com.android.hedgehog;

import com.scoreloop.client.android.core.controller.RequestController;
import com.scoreloop.client.android.core.controller.RequestControllerObserver;
import com.scoreloop.client.android.core.controller.ScoreController;
import com.scoreloop.client.android.core.controller.TermsOfServiceController;
import com.scoreloop.client.android.core.controller.TermsOfServiceControllerObserver;
import com.scoreloop.client.android.core.model.Score;
import com.scoreloop.client.android.core.model.Session;
import com.scoreloop.client.android.core.model.TermsOfService;
import com.scoreloop.client.android.core.model.TermsOfService.Status;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.Toast;

public class SubmitScore2 extends Activity {
	// identifiers for our dialogues
	static final int DIALOG_PROGRESS = 0;
	static final int DIALOG_SUBMITTED = 1;
	static final int DIALOG_FAILED = 2;
	double playerScore = 300;
	ScoreManager sm = new ScoreManager();
	TermsOfServiceControllerObserver observer;
	TermsOfService tos = Session.getCurrentSession().getUsersTermsOfService();
	TermsOfServiceController controller;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (tos.getStatus() == Status.ACCEPTED) {// ACCEPTED

			submitScore();

		}// /////////ACCEPTED

		else {// ////////////////NOT ACCEPTED
			observer = new TermsOfServiceControllerObserver() {// CONTROLLEROBSERVER

				@Override
				public void termsOfServiceControllerDidFinish(
						TermsOfServiceController toscontroller, Boolean accepted) {

					try {// //////TRY&CATCH

						if (accepted) {
							submitScore();
						} else {
							Toast.makeText(getApplicationContext(),
									"must accept terms to check rankings",
									Toast.LENGTH_LONG).show();
							finish();
						}

					} catch (NullPointerException e) {
						Toast.makeText(getApplicationContext(),
								"No Internet Connection", Toast.LENGTH_LONG)
								.show();

					}// /////////TRY&CATCH

				}

			};// CONTROLLEROBSERVER
			controller = new TermsOfServiceController(observer);
			controller.query(getParent());
		}// ////////////////////NOT ACCEPTED

	}// ONCREATE

	private void submitScore() {

		RequestControllerObserver rcObserver = new RequestControllerObserver() {

			@Override
			public void requestControllerDidFail(RequestController rcontroller,
					Exception arg1) { // internet problem
				dismissDialog(DIALOG_PROGRESS);
				Toast.makeText(getApplicationContext(),
						"try checking your connection", Toast.LENGTH_LONG)
						.show();
				finish();

			}

			@Override
			public void requestControllerDidReceiveResponse(
					RequestController controller) {
				dismissDialog(DIALOG_PROGRESS);
				sm.uploadScore(1400); // now upload IQ

				Toast.makeText(getApplicationContext(), "Success!",
						Toast.LENGTH_SHORT).show();
				finish();

			}

		};// REQUESTCONTROLLEROBSERVER

		Score score = new Score((double) Settings.cyberScore, null);
		score.setMode(0);
		ScoreController scoreController = new ScoreController(rcObserver);
		showDialog(DIALOG_PROGRESS);
		scoreController.submitScore(score);

	}

	// handler to create our dialogs
	protected Dialog onCreateDialog(int id) {
		switch (id) {
			case DIALOG_PROGRESS :
				return ProgressDialog.show(SubmitScore2.this, "",
						"Getting rank");
			case DIALOG_SUBMITTED :
				return (new AlertDialog.Builder(this))
						.setMessage("rank obtained")
						.setTitle("scoreloop")
						.setIcon(
								getResources().getDrawable(
										R.drawable.ic_launcher))
						.setPositiveButton("awesome", null).create();
			case DIALOG_FAILED :
				return (new AlertDialog.Builder(this)).setMessage("error")
						.setPositiveButton("too bad", null).create();
		}
		return null;
	}

}
